package com.hf.eagle.web.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.hf.eagle.service.Facade;

/**
 * @author Xu,XiaoYuan
 */
public class ResourceListener implements ServletContextListener {
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());

	public void contextDestroyed(ServletContextEvent sec) {
		ServletContext application = sec.getServletContext();
		application.removeAttribute("c_n");
		application.removeAttribute("html_br");
		application.removeAttribute("sys_encoding");
	}

	public void contextInitialized(ServletContextEvent sec) {
		ServletContext servletContext = sec.getServletContext();

		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
		Facade facade = (Facade) wac.getBean("facade");

		initInternalResource(sec, facade);
	}

	public void initInternalResource(ServletContextEvent sec, Facade facade) {
		ServletContext application = sec.getServletContext();
		application.setAttribute("c_n", "\n");
		application.setAttribute("html_br", "<br />");
		application.setAttribute("sys_encoding", "UTF-8");

	}

}
